#include <Wire.h>

// DS3231 I2C 地址
#define DS3231_ADDRESS 0x68

void setup() {
  Serial.begin(9600);
  Wire.begin();

  // 初始化 DS3231
  Wire.beginTransmission(DS3231_ADDRESS);
  Wire.write(0x00); // 设置寄存器为时间寄存器
  Wire.endTransmission();
}

void loop() {
  // 读取 DS3231 的时间
  Wire.beginTransmission(DS3231_ADDRESS);
  Wire.write(0x00); // 设置寄存器为时间寄存器
  Wire.endTransmission(false);
  Wire.requestFrom(DS3231_ADDRESS, 7); // 读取 7 个字节的数据
  int year = Wire.read() << 8 | Wire.read();
  int month = Wire.read();
  int day = Wire.read();
  int hour = Wire.read();
  int minute = Wire.read();
  int second = Wire.read();

  // 将 DS3231 的时间打印到串口监视器
  Serial.print("Year: ");
  Serial.print(year);
  Serial.print(" Month: ");
  Serial.print(month);
  Serial.print(" Day: ");
  Serial.print(day);
  Serial.print(" Hour: ");
  Serial.print(hour);
  Serial.print(" Minute: ");
  Serial.print(minute);
  Serial.print(" Second: ");
  Serial.println(second);

  delay(1000);
}